package com.jlpay.openapi.demo.trans.refunds;

import com.jlpay.open.jlpay.sdk.java.http.JlpayHttpHeaders;
import com.jlpay.open.jlpay.sdk.java.model.notify.NotifyParam;
import com.jlpay.open.jlpay.sdk.java.model.trans.refunds.request.RefundsNotifyMessage;
import com.jlpay.open.jlpay.sdk.java.utils.json.JsonUtils;
import com.jlpay.openapi.demo.JlpayOpenApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author zhangyongfeng
 * @date 2025/3/18
 */
public class RefundsNotifyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RefundsNotifyService.class);

    public static void main(String[] args) {
        String notifyBody = "{\"mch_id\":\"849581183510017\",\"out_trade_no\":\"202503185000051zyfTest\",\"transaction_id\":\"451137966813128219033635\",\"chn_transaction_id\":\"012025031822001428091442454111\",\"ori_out_trade_no\":\"20250318100005zyfTest\",\"ori_transaction_id\":\"451137966737802815078416\",\"total_fee\":\"1\",\"status\":\"2\",\"summary\":\"成功\",\"trans_time\":\"2025-03-18 13:54:18\",\"success_time\":\"2025-03-18 13:54:18\"}";
        //报文头
        Map<String, String> headers = getHeaders();

        NotifyParam notifyParam = NotifyParam.builder()
                .method("POST")
                .uri("/jlpay/refunds/notify")
                .alg(headers.get(JlpayHttpHeaders.V5.SIGN_ALG))
                .sign(headers.get(JlpayHttpHeaders.V5.SIGN))
                .nonce(headers.get(JlpayHttpHeaders.V5.NONCE))
                .timestamp(headers.get(JlpayHttpHeaders.V5.TIMESTAMP))
                .body(notifyBody)
                .build();

        RefundsNotifyMessage notifyMessage = JlpayOpenApiService.notifyParseService()
                .parse(notifyParam, RefundsNotifyMessage.class);
        LOGGER.info("退款申请通知响应:{}", JsonUtils.toString(notifyMessage));
    }

    private static Map<String, String> getHeaders() {
        Map<String, String> headers = new HashMap<>();
        headers.put(JlpayHttpHeaders.V5.TIMESTAMP, "1742277264");
        headers.put(JlpayHttpHeaders.V5.NONCE, "pYpsiS6wovCvPH1Pp8pryBF7a2VGNovX");
        headers.put(JlpayHttpHeaders.V5.SIGN_ALG, "SM3WithSM2WithDer");
        headers.put(JlpayHttpHeaders.V5.SIGN,
                "MEYCIQCfF9rhlH7ecWfjVM0RsqXTI7fhxnPmQwBTljUa0rrS3AIhAO+c6RVKhtHQChPktArCx948Y5qDcX1OKEk5lBmr5HUD");
        return headers;
    }

    private static String getSignContext(Map<String, Object> contextMap) {
        TreeMap<String, Object> signData = new TreeMap<>(contextMap);
        return JsonUtils.toString(signData);
    }
}
